{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Direction</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Region</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Size</th>\n",
       "      <th>age</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>75.0</td>\n",
       "      <td>33</td>\n",
       "      <td>780.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>60.0</td>\n",
       "      <td>33</td>\n",
       "      <td>705.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>210.0</td>\n",
       "      <td>25</td>\n",
       "      <td>1400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>39.0</td>\n",
       "      <td>17</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>90.0</td>\n",
       "      <td>11</td>\n",
       "      <td>998.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Direction  Elevator  Floor  Layout  Region  Renovation   Size  age   Price\n",
       "0          3         2      6      16       0           3   75.0   33   780.0\n",
       "1          9         0      6       9       0           3   60.0   33   705.0\n",
       "2         10         1     16      16       0           0  210.0   25  1400.0\n",
       "3          7         2      7       2       0           3   39.0   17   420.0\n",
       "4          7         1     19      10       0           3   90.0   11   998.0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "f = open('建模数据.csv')\n",
    "file = pd.read_csv(f, encoding = 'utf-8')\n",
    "file.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Direction</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Region</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Size</th>\n",
       "      <th>age</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>75.0</td>\n",
       "      <td>33</td>\n",
       "      <td>780.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>60.0</td>\n",
       "      <td>33</td>\n",
       "      <td>705.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>210.0</td>\n",
       "      <td>25</td>\n",
       "      <td>1400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>39.0</td>\n",
       "      <td>17</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>90.0</td>\n",
       "      <td>11</td>\n",
       "      <td>998.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Direction  Elevator  Floor  Layout  Region  Renovation   Size  age   Price\n",
       "0          3         2      6      16       0           3   75.0   33   780.0\n",
       "1          9         0      6       9       0           3   60.0   33   705.0\n",
       "2         10         1     16      16       0           0  210.0   25  1400.0\n",
       "3          7         2      7       2       0           3   39.0   17   420.0\n",
       "4          7         1     19      10       0           3   90.0   11   998.0"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_list = ['Direction', 'Elevator', 'Floor', 'Layout', 'Region', 'Renovation', 'Size', 'age', 'Price']\n",
    "file_a = file[col_list]\n",
    "file_a.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用toad库进行逐步回归筛选变量，以7:3比例划分训练集和测试集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import toad\n",
    "\n",
    "data_train = file_a.iloc[0: int(len(file_a) * 0.8)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "stepwise_selected = toad.selection.stepwise(data_train, target = 'Price', estimator = 'ols', direction = 'both', criterion = 'aic', exclude = [])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Direction', 'Elevator', 'Floor', 'Layout', 'Renovation', 'Size', 'age']"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_list = [i for i in stepwise_selected.columns if i not in ['Price']]\n",
    "feature_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的结果是入模变量，导入statsmodels库，进行OLS建模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Direction</th>\n",
       "      <th>Elevator</th>\n",
       "      <th>Floor</th>\n",
       "      <th>Layout</th>\n",
       "      <th>Renovation</th>\n",
       "      <th>Size</th>\n",
       "      <th>age</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>16</td>\n",
       "      <td>3</td>\n",
       "      <td>75.0</td>\n",
       "      <td>33</td>\n",
       "      <td>780.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>3</td>\n",
       "      <td>60.0</td>\n",
       "      <td>33</td>\n",
       "      <td>705.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10</td>\n",
       "      <td>1</td>\n",
       "      <td>16</td>\n",
       "      <td>16</td>\n",
       "      <td>0</td>\n",
       "      <td>210.0</td>\n",
       "      <td>25</td>\n",
       "      <td>1400.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>39.0</td>\n",
       "      <td>17</td>\n",
       "      <td>420.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>7</td>\n",
       "      <td>1</td>\n",
       "      <td>19</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>90.0</td>\n",
       "      <td>11</td>\n",
       "      <td>998.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Direction  Elevator  Floor  Layout  Renovation   Size  age   Price\n",
       "0          3         2      6      16           3   75.0   33   780.0\n",
       "1          9         0      6       9           3   60.0   33   705.0\n",
       "2         10         1     16      16           0  210.0   25  1400.0\n",
       "3          7         2      7       2           3   39.0   17   420.0\n",
       "4          7         1     19      10           3   90.0   11   998.0"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_list.append('Price')\n",
    "data_train = data_train[feature_list]\n",
    "data_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = sm.add_constant(data_train.iloc[:,:len(feature_list) - 1]) #生成自变量\n",
    "y = data_train['Price'] #生成因变量\n",
    "model = sm.OLS(y, x) #生成模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "result = model.fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<table class=\"simpletable\">\n",
       "<caption>OLS Regression Results</caption>\n",
       "<tr>\n",
       "  <th>Dep. Variable:</th>          <td>Price</td>      <th>  R-squared:         </th>  <td>   0.549</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Model:</th>                   <td>OLS</td>       <th>  Adj. R-squared:    </th>  <td>   0.549</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Method:</th>             <td>Least Squares</td>  <th>  F-statistic:       </th>  <td>   3086.</td>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Date:</th>             <td>Fri, 30 Apr 2021</td> <th>  Prob (F-statistic):</th>   <td>  0.00</td>   \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Time:</th>                 <td>17:51:57</td>     <th>  Log-Likelihood:    </th> <td>-1.2208e+05</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>No. Observations:</th>      <td> 17720</td>      <th>  AIC:               </th>  <td>2.442e+05</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Residuals:</th>          <td> 17712</td>      <th>  BIC:               </th>  <td>2.442e+05</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Df Model:</th>              <td>     7</td>      <th>                     </th>      <td> </td>     \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Covariance Type:</th>      <td>nonrobust</td>    <th>                     </th>      <td> </td>     \n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "       <td></td>         <th>coef</th>     <th>std err</th>      <th>t</th>      <th>P>|t|</th>  <th>[0.025</th>    <th>0.975]</th>  \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>const</th>      <td> -272.3428</td> <td>   10.776</td> <td>  -25.273</td> <td> 0.000</td> <td> -293.465</td> <td> -251.221</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Direction</th>  <td>   -1.0507</td> <td>    0.579</td> <td>   -1.816</td> <td> 0.069</td> <td>   -2.185</td> <td>    0.084</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Elevator</th>   <td>    8.9828</td> <td>    2.407</td> <td>    3.733</td> <td> 0.000</td> <td>    4.266</td> <td>   13.700</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Floor</th>      <td>    7.9654</td> <td>    0.266</td> <td>   29.968</td> <td> 0.000</td> <td>    7.444</td> <td>    8.486</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Layout</th>     <td>   -2.1660</td> <td>    0.398</td> <td>   -5.444</td> <td> 0.000</td> <td>   -2.946</td> <td>   -1.386</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Renovation</th> <td>   20.7224</td> <td>    1.796</td> <td>   11.537</td> <td> 0.000</td> <td>   17.202</td> <td>   24.243</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Size</th>       <td>    5.6892</td> <td>    0.056</td> <td>  102.044</td> <td> 0.000</td> <td>    5.580</td> <td>    5.799</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>age</th>        <td>    6.7852</td> <td>    0.249</td> <td>   27.261</td> <td> 0.000</td> <td>    6.297</td> <td>    7.273</td>\n",
       "</tr>\n",
       "</table>\n",
       "<table class=\"simpletable\">\n",
       "<tr>\n",
       "  <th>Omnibus:</th>       <td>8448.261</td> <th>  Durbin-Watson:     </th>  <td>   1.252</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Prob(Omnibus):</th>  <td> 0.000</td>  <th>  Jarque-Bera (JB):  </th> <td>192525.856</td>\n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Skew:</th>           <td> 1.778</td>  <th>  Prob(JB):          </th>  <td>    0.00</td> \n",
       "</tr>\n",
       "<tr>\n",
       "  <th>Kurtosis:</th>       <td>18.751</td>  <th>  Cond. No.          </th>  <td>    694.</td> \n",
       "</tr>\n",
       "</table><br/><br/>Warnings:<br/>[1] Standard Errors assume that the covariance matrix of the errors is correctly specified."
      ],
      "text/plain": [
       "<class 'statsmodels.iolib.summary.Summary'>\n",
       "\"\"\"\n",
       "                            OLS Regression Results                            \n",
       "==============================================================================\n",
       "Dep. Variable:                  Price   R-squared:                       0.549\n",
       "Model:                            OLS   Adj. R-squared:                  0.549\n",
       "Method:                 Least Squares   F-statistic:                     3086.\n",
       "Date:                Fri, 30 Apr 2021   Prob (F-statistic):               0.00\n",
       "Time:                        17:51:57   Log-Likelihood:            -1.2208e+05\n",
       "No. Observations:               17720   AIC:                         2.442e+05\n",
       "Df Residuals:                   17712   BIC:                         2.442e+05\n",
       "Df Model:                           7                                         \n",
       "Covariance Type:            nonrobust                                         \n",
       "==============================================================================\n",
       "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
       "------------------------------------------------------------------------------\n",
       "const       -272.3428     10.776    -25.273      0.000    -293.465    -251.221\n",
       "Direction     -1.0507      0.579     -1.816      0.069      -2.185       0.084\n",
       "Elevator       8.9828      2.407      3.733      0.000       4.266      13.700\n",
       "Floor          7.9654      0.266     29.968      0.000       7.444       8.486\n",
       "Layout        -2.1660      0.398     -5.444      0.000      -2.946      -1.386\n",
       "Renovation    20.7224      1.796     11.537      0.000      17.202      24.243\n",
       "Size           5.6892      0.056    102.044      0.000       5.580       5.799\n",
       "age            6.7852      0.249     27.261      0.000       6.297       7.273\n",
       "==============================================================================\n",
       "Omnibus:                     8448.261   Durbin-Watson:                   1.252\n",
       "Prob(Omnibus):                  0.000   Jarque-Bera (JB):           192525.856\n",
       "Skew:                           1.778   Prob(JB):                         0.00\n",
       "Kurtosis:                      18.751   Cond. No.                         694.\n",
       "==============================================================================\n",
       "\n",
       "Warnings:\n",
       "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
       "\"\"\""
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result.summary()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
